<template>
  <div style="width:100%;overflow-x:hidden;overflow-y:auto">
    <input v-model="testCnt">
    <div style="display:flex">
      <div style="flex:1">
        <div
          style="display:inline-block;border:1px solid;width:50px;height:80px;margin-right:10px;margin-bottom:10px;font-size:26px;position:relative"
          :style="{color:['black', 'red', 'green', 'blue'][card == -1 ? 0 : (card / 13 | 0)], borderColor:['black', 'red', 'green', 'blue'][card == -1 ? 0 : (card / 13 | 0)]}"
          v-for="(card, index) in handCards"
          @click="onHandCardClick(index)">
          {{card != -1 ? '♠♥♣♦'[card / 13 | 0] + (card % 13 < 9 ? card % 13 + 2 : 'TJQKA'[card % 13 - 8]) : '　'}}
          <div style="position:absolute;bottom:0;right:0" @click.stop="handCards.splice(index, 1, -1)">×</div>
        </div>
        <button @click="handCards.splice(0, 2, -1, -1)">clear</button>
      </div>
      <div style="flex:1">
        <div
          style="display:inline-block;border:1px solid;width:50px;height:80px;margin-right:10px;margin-bottom:10px;font-size:26px;position:relative"
          :style="{color:['black', 'red', 'green', 'blue'][card == -1 ? 0 : (card / 13 | 0)], borderColor:['black', 'red', 'green', 'blue'][card == -1 ? 0 : (card / 13 | 0)]}"
          v-for="(card, index) in boardCards"
          @click="onBoardCardClick(index)">
          {{card != -1 ? '♠♥♣♦'[card / 13 | 0] + (card % 13 < 9 ? card % 13 + 2 : 'TJQKA'[card % 13 - 8]) : '　'}}
          <div style="position:absolute;bottom:0;right:0" @click.stop="boardCards.splice(index, 1, -1)">×</div>
        </div>
        <button @click="boardCards.splice(0, 5, -1, -1, -1, -1, -1)">clear</button>
      </div>
      <div style="flex:1">
        <div
          style="display:inline-block;border:1px solid;width:50px;height:80px;margin-right:10px;margin-bottom:10px;font-size:26px;position:relative"
          :style="{color:['black', 'red', 'green', 'blue'][card == -1 ? 0 : (card / 13 | 0)], borderColor:['black', 'red', 'green', 'blue'][card == -1 ? 0 : (card / 13 | 0)]}"
          v-for="(card, index) in otherHandCards"
          @click="onOtherHandCardClick(index)">
          {{card != -1 ? '♠♥♣♦'[card / 13 | 0] + (card % 13 < 9 ? card % 13 + 2 : 'TJQKA'[card % 13 - 8]) : '　'}}
          <div style="position:absolute;bottom:0;right:0" @click.stop="otherHandCards.splice(index, 1, -1)">×</div>
        </div>
        <button @click="otherHandCards.splice(0, 2, -1, -1)">clear</button>
      </div>
    </div>
    <div>{{winRate}}</div>
    <div>
      <button @click="calc">calc</button>
    </div>
    <div style="display:flex" v-for="item in testList">
      <div style="flex:1">{{item.handCards}}</div>
      <div style="flex:1">{{item.boardCards}}</div>
      <div style="flex:1">{{item.otherHandCards}}</div>
      <div style="flex:1">{{item.lose}}</div>
    </div>
    <weui-dialog title="" :list="[]" :width="350" v-show="pokerPicker.isShow" @on-mask="pokerPicker.isShow=false">
      <div style="margin:-1.9em -1.6em -.8em;">
        <div style="display:flex;" v-for="(color, index) in '♠♥♣♦'">
          <div style="flex:1;border:1px solid"
               :style="{color:['black', 'red', 'green', 'blue'][index], borderColor:['black', 'red', 'green', 'blue'][index]}"
               v-for="i in 13"
               @click="pokerPicker.resolve(index * 13 + i - 1)">
            {{color + (i < 9 ? i + 1 : 'TJQKA'[i - 9])}}
          </div>
        </div>
      </div>
    </weui-dialog>
  </div>
</template>

<script>
  import WeuiDialog from '../vue2componentSet/src/components/WeuiDialog'

  export default {
    components: {
      WeuiDialog
    },
    data() {
      return {
        testCnt: 5000,
        handCards: [-1, -1],
        boardCards: [-1, -1, -1, -1, -1],
        otherHandCards: [-1, -1],
        winRate: '',
        testList: [],
        pokerPicker: {
          isShow: false,
          resolve() {
          }
        }
      }
    },
    mounted() {
      this.$store.commit('set_title', 'Texas Calc')
    },
    methods: {
      calc() {
        var date = new Date()
        var Poker = function (n) {
          this.num = (n % 13) + 2;
          this.ch = this.num > 9 ? ("abcdef"[this.num - 10]) : this.num;
          this.show = "0,1,2,3,4,5,6,7,8,9,10,J,Q,K,A".split(",")[this.num];
          this.color = "♠♥♣♦"[n / 13 | 0];
          this.toString = function () {
            return this.color + this.show;
          };
        };
        var z = function (arg) { //同花顺
          var a = arg, t = a[0];
          for (var i = 1; i < a.length; i++) {
            if (a[i].color === t.color && a[i].num === t.num + 1) {
              t = a[i];
            } else if (i === a.length - 1 && t.num === 5 && a[i].num === 14 && a[i].color === t.color) {
              t = a[0];
            } else {
              return;
            }
          }
          return "z" + t.ch;
        };
        var y = function (arg) { //四条
          var a = arg.map(function (p) {
            return p.ch;
          }).join(""), mat = a.match(/(\w*)(\w)\2\2\2(\w*)/);
          if (mat) {
            return "y" + mat[2] + mat[1] + mat[3];
          }
        };
        var x = function (arg) { //葫芦
          var a = arg.map(function (p) {
            return p.ch;
          }).join(""), mat;
          if (mat = a.match(/(\w)\1(\w)\2\2/)) {
            return "x" + mat[2] + mat[1];
          } else if (mat = a.match(/(\w)\1\1(\w)\2/)) {
            return "x" + mat[1] + mat[2];
          }
        };
        var w = function (arg) { //同花
          var a = arg, t = a[0];
          for (var i = 1; i < a.length; i++) {
            if (a[i].color === t.color) {
              t = a[i];
            } else {
              return;
            }
          }
          return "w" + arg.map(function (p) {
            return p.ch;
          }).reverse().join("");
        };
        var v = function (arg) { //顺子
          var a = arg, t = a[0];
          for (var i = 1; i < a.length; i++) {
            if (a[i].num === t.num + 1) {
              t = a[i];
            } else if (i === a.length - 1 && t.num === 5 && a[i].num === 14) {
              t = a[0];
            } else {
              return;
            }
          }
          return "v" + t.ch;
        };
        var u = function (arg) { //三条
          var a = arg.map(function (p) {
            return p.ch;
          }).join(""), mat;
          if (mat = a.match(/(\w*)(\w)\2\2(\w*)/)) {
            return "u" + mat[2] + (mat[3] + mat[1]).split("").reverse().join("");
          }
        };
        var t = function (arg) { //两对
          var a = arg.map(function (p) {
            return p.ch;
          }).join(""), mat;
          if (mat = a.match(/(\w*)(\w)\2(\w)\3(\w*)/)) {
            return "t" + mat[3] + mat[2] + mat[4] + mat[1];
          } else if (mat = a.match(/(\w)\1(\w)(\w)\3/)) {
            return "t" + mat[3] + mat[1] + mat[2];
          }
        };
        var s = function (arg) { //一对
          var a = arg.map(function (p) {
            return p.ch;
          }).join(""), mat;
          if (mat = a.match(/(\w*)(\w)\2(\w*)/)) {
            return "s" + mat[2] + (mat[1] + mat[3]).split("").reverse().join("");
          }
        };
        var r = function (arg) { //高牌
          return "r" + arg.map(function (p) {
            return p.ch;
          }).reverse().join("");
        };
        var score = function () {
          var a = [].map.call(arguments.length == 1 ? arguments[0] : arguments, function (p) {
            return new Poker(p);
          }).sort(function (p1, p2) {
            return p1.num - p2.num;
          });
          return z(a) || y(a) || x(a) || w(a) || v(a) || u(a) || t(a) || s(a) || r(a);
        };
        var pickedHandCards = []
        for (var i = 0; i < this.handCards.length; i++) {
          if (this.handCards[i] === -1)
            continue
          if (pickedHandCards.indexOf(this.handCards[i]) == -1)
            pickedHandCards.push(this.handCards[i])
          else {
            this.winRate = '第' + (i + 1) + '张手牌重复了'
            return
          }
        }
        var pickedBoardCards = []
        for (var i = 0; i < this.boardCards.length; i++) {
          if (this.boardCards[i] === -1)
            continue
          if (pickedHandCards.indexOf(this.boardCards[i]) == -1 && pickedBoardCards.indexOf(this.boardCards[i]) == -1)
            pickedBoardCards.push(this.boardCards[i])
          else {
            this.winRate = '第' + (i + 1) + '张公共牌重复了'
            return
          }
        }
        var pickedOtherHandCards = []
        for (var i = 0; i < this.otherHandCards.length; i++) {
          if (this.otherHandCards[i] === -1)
            continue
          if (pickedHandCards.indexOf(this.otherHandCards[i]) == -1 && pickedBoardCards.indexOf(this.otherHandCards[i]) == -1 && pickedOtherHandCards.indexOf(this.otherHandCards[i]) == -1)
            pickedOtherHandCards.push(this.otherHandCards[i])
          else {
            this.winRate = '第' + (i + 1) + '张牌重复了'
            return
          }
        }
        var testCnt = 1
        for (var i = 0; i < 2 - pickedHandCards.length; i++) {
          testCnt *= (52 - pickedHandCards.length - pickedBoardCards.length - pickedOtherHandCards.length - i) / (i + 1)
        }
        for (var i = 0; i < 5 - pickedBoardCards.length; i++) {
          testCnt *= (52 - 2 - pickedBoardCards.length - pickedOtherHandCards.length - i) / (i + 1)
        }
        for (var i = 0; i < 2 - pickedOtherHandCards.length; i++) {
          testCnt *= (52 - 2 - 5 - pickedOtherHandCards.length - i) / (i + 1)
        }
        var lose = 0
        var handCards = []
        pickedHandCards.forEach(card => {
          handCards.push(card)
        })
        var boardCards = []
        pickedBoardCards.forEach(card => {
          boardCards.push(card)
        })
        var otherHandCards = []
        pickedOtherHandCards.forEach(card => {
          otherHandCards.push(card)
        })
        this.testList = []
        for (var testNo = 0; testNo < this.testCnt && testNo < testCnt; testNo++) {
          if (this.testCnt < testCnt) {
            handCards.splice(pickedHandCards.length, 2)
            boardCards.splice(pickedBoardCards.length, 5)
            otherHandCards.splice(pickedOtherHandCards.length, 2)
            while (1) {
              var card = (Math.random() * 52 | 0) % 52
              if (handCards.indexOf(card) == -1 && boardCards.indexOf(card) == -1 && otherHandCards.indexOf(card) == -1) {
                if (handCards.length < 2) {
                  handCards.push(card)
                } else if (boardCards.length < 5) {
                  boardCards.push(card)
                } else if (otherHandCards.length < 2) {
                  otherHandCards.push(card)
                } else {
                  break
                }
              }
            }
          } else {
            if (testNo == 0) {
              for (var card = 0; card < 52; card++) {
                if (handCards.indexOf(card) == -1 && boardCards.indexOf(card) == -1 && otherHandCards.indexOf(card) == -1) {
                  if (handCards.length < 2) {
                    handCards.push(card)
                  } else if (boardCards.length < 5) {
                    boardCards.push(card)
                  } else if (otherHandCards.length < 2) {
                    otherHandCards.push(card)
                  } else {
                    break
                  }
                }
              }
            } else {
              var finish = false
              while (1) {
                for (var i = 1; i >= pickedOtherHandCards.length; i--) {
                  while (1) {
                    otherHandCards[i]++
                    if (handCards.indexOf(otherHandCards[i]) == -1 && boardCards.indexOf(otherHandCards[i]) == -1 && pickedOtherHandCards.indexOf(otherHandCards[i]) == -1)
                      break
                    if (otherHandCards[i] == 51 + i)
                      break
                  }
                  if (otherHandCards[i] == 51 + i)
                    continue
                  finish = true
                  for (i++; i < 2; i++) {
                    otherHandCards[i] = otherHandCards[i - 1]
                    while (1) {
                      otherHandCards[i]++
                      if (handCards.indexOf(otherHandCards[i]) == -1 && boardCards.indexOf(otherHandCards[i]) == -1 && pickedOtherHandCards.indexOf(otherHandCards[i]) == -1)
                        break
                      if (otherHandCards[i] == 51 + i)
                        break
                    }
                    if (otherHandCards[i] == 51 + i) {
                      finish = false
                      break
                    }
                  }
                  if (finish)
                    break
                }
                if (finish)
                  break
                for (var i = 4; i >= pickedBoardCards.length; i--) {
                  while (1) {
                    boardCards[i]++
                    if (handCards.indexOf(boardCards[i]) == -1 && pickedBoardCards.indexOf(boardCards[i]) == -1 && pickedOtherHandCards.indexOf(boardCards[i]) == -1)
                      break
                    if (boardCards[i] == 48 + i)
                      break
                  }
                  if (boardCards[i] == 48 + i)
                    continue
                  finish = true
                  for (i++; i < 5; i++) {
                    boardCards[i] = boardCards[i - 1]
                    while (1) {
                      boardCards[i]++
                      if (handCards.indexOf(boardCards[i]) == -1 && pickedBoardCards.indexOf(boardCards[i]) == -1 && pickedOtherHandCards.indexOf(boardCards[i]) == -1)
                        break
                      if (boardCards[i] == 48 + i)
                        break
                    }
                    if (boardCards[i] == 48 + i) {
                      finish = false
                      break
                    }
                  }
                  if (finish) {
                    for (var j = 0; j < 2; j++) {
                      otherHandCards[j] = j == 0 ? -1 : otherHandCards[j - 1]
                      while (1) {
                        otherHandCards[j]++
                        if (handCards.indexOf(otherHandCards[j]) == -1 && boardCards.indexOf(otherHandCards[j]) == -1 && pickedOtherHandCards.indexOf(otherHandCards[j]) == -1)
                          break
                        if (otherHandCards[j] == 51 + j)
                          break
                      }
                      if (otherHandCards[j] == 51 + j) {
                        finish = false
                        break
                      }
                    }
                    if (finish)
                      break
                  }
                }
                if (finish)
                  break
                for (var i = 1; i >= pickedHandCards.length; i--) {
                  while (1) {
                    handCards[i]++
                    if (pickedHandCards.indexOf(handCards[i]) == -1 && pickedBoardCards.indexOf(handCards[i]) == -1 && pickedOtherHandCards.indexOf(handCards[i]) == -1)
                      break
                    if (handCards[i] == 51 + i)
                      break
                  }
                  if (handCards[i] == 51 + i)
                    continue
                  finish = true
                  for (i++; i < 2; i++) {
                    handCards[i] = handCards[i - 1]
                    while (1) {
                      handCards[i]++
                      if (pickedHandCards.indexOf(handCards[i]) == -1 && pickedBoardCards.indexOf(handCards[i]) == -1 && pickedOtherHandCards.indexOf(handCards[i]) == -1)
                        break
                      if (handCards[i] == 51 + i)
                        break
                    }
                    if (handCards[i] == 51 + i) {
                      finish = false
                      break
                    }
                  }
                  if (finish) {
                    for (var j = 0; j < 4; j++) {
                      boardCards[j] = j == 0 ? -1 : boardCards[j - 1]
                      while (1) {
                        boardCards[j]++
                        if (handCards.indexOf(boardCards[j]) == -1 && pickedBoardCards.indexOf(boardCards[j]) == -1 && pickedOtherHandCards.indexOf(boardCards[j]) == -1)
                          break
                        if (boardCards[j] == 48 + j)
                          break
                      }
                      if (boardCards[j] == 48 + j) {
                        finish = false
                        break
                      }
                    }
                    if (finish) {
                      for (var j = 0; j < 2; j++) {
                        otherHandCards[j] = j == 0 ? -1 : otherHandCards[j - 1]
                        while (1) {
                          otherHandCards[j]++
                          if (handCards.indexOf(otherHandCards[j]) == -1 && boardCards.indexOf(otherHandCards[j]) == -1 && pickedOtherHandCards.indexOf(otherHandCards[j]) == -1)
                            break
                          if (otherHandCards[j] == 51 + j)
                            break
                        }
                        if (otherHandCards[j] == 51 + j) {
                          finish = false
                          break
                        }
                      }
                      if (finish)
                        break
                    }
                  }
                }
                if (finish)
                  break
              }
            }
          }
          var bestScore = ""
          for (var i = 0; i < 7; i++) {
            for (var j = i + 1; j < 7; j++) {
              var cards = []
              handCards.forEach(card => {
                cards.push(card)
              })
              boardCards.forEach(card => {
                cards.push(card)
              })
              cards.splice(j, 1)
              cards.splice(i, 1)
              var thisScore = score(cards);
              if (bestScore < thisScore)
                bestScore = thisScore
            }
          }
          var hasResult = false
          for (var i = 0; i < 7; i++) {
            for (var j = i + 1; j < 7; j++) {
              var cards = []
              otherHandCards.forEach(card => {
                cards.push(card)
              })
              boardCards.forEach(card => {
                cards.push(card)
              })
              cards.splice(j, 1)
              cards.splice(i, 1)
              var thisScore = score(cards)
              if (bestScore < thisScore) {
                lose++
                hasResult = true
                break
              }
            }
            if (hasResult)
              break
          }
          this.testList.push({
            handCards: handCards.map(p => {
              return new Poker(p);
            }).sort(function (p1, p2) {
              return p1.num - p2.num;
            }).join(','),
            boardCards: boardCards.map(p => {
              return new Poker(p);
            }).sort(function (p1, p2) {
              return p1.num - p2.num;
            }).join(','),
            otherHandCards: otherHandCards.map(p => {
              return new Poker(p);
            }).sort(function (p1, p2) {
              return p1.num - p2.num;
            }).join(','),
            lose: hasResult
          })
        }
        this.winRate = (1 - lose / (this.testCnt < testCnt ? this.testCnt : testCnt)) + ' takes ' + (new Date().getTime() - date.getTime()) + ' ms'
      },
      onHandCardClick(index) {
        this.pokerPicker.isShow = true
        this.pokerPicker.resolve = card => {
          this.handCards[index] = card
          this.pokerPicker.isShow = false
        }
      },
      onBoardCardClick(index) {
        this.pokerPicker.isShow = true
        this.pokerPicker.resolve = card => {
          this.boardCards[index] = card
          this.pokerPicker.isShow = false
        }
      },
      onOtherHandCardClick(index) {
        this.pokerPicker.isShow = true
        this.pokerPicker.resolve = card => {
          this.otherHandCards[index] = card
          this.pokerPicker.isShow = false
        }
      },
    }
  }
</script>
